import React from 'react'
import {
    Switch,
    Route,
    Redirect
} from 'react-router-dom'

export default function RouterView(props) {
    let routes = props.routes.filter(item => item.component)
    let redirect = props.routes.filter(item => item.redirect)

    return (
        <Switch>
            {
                routes.map(item => <Route key={item.path} path={item.path} render={props => {
                    if (item.children) {
                        return <item.component routes={item.children} {...props} />
                    }
                    return <item.component {...props} />
                }} />)
            }
            {
                redirect.map(item => <Redirect key={item.path} from={item.path} to={item.redirect} />)
            }
        </Switch>
    )
}
